from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.viewsets import GenericViewSet
from rest_framework import serializers
from apps.minapp.models import OrderInfo, OrderGoods, Detail
from django_filters.rest_framework import FilterSet, DjangoFilterBackend, ChoiceFilter, CharFilter
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter


class OrderFilter(FilterSet):
    ORDER_STATUS_CHOICES = (
        (1, "待支付"),
        (2, "待接单"),
        (3, "制作中"),
        (4, "待取餐"),
        (5, "已完成"),
        (6, "已取消"),
    )
    status = ChoiceFilter(choices=ORDER_STATUS_CHOICES)
    user = CharFilter(field_name='user_id__studentid', lookup_expr='contains')
    order_sn = CharFilter(lookup_expr='contains')
    created_time = CharFilter(lookup_expr='contains')
    pay_time = CharFilter(lookup_expr='contains')

    class Meta:
        model = OrderInfo
        fields = ['status', 'order_sn', 'created_time', 'pay_time']


class OrderListSerializer(serializers.ModelSerializer):
    created_time = serializers.CharField()
    pay_time = serializers.CharField()
    user = serializers.SerializerMethodField()
    status = serializers.CharField(source='get_status_display')

    @staticmethod
    def get_user(obj):
        return obj.user_id.studentid

    class Meta:
        model = OrderInfo
        fields = ['id', 'created_time', 'pay_time', 'order_sn', 'status', 'total_amount', 'total_count', 'user']


class GoodsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Detail
        fields = "__all__"


class OrderGoodsSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    description = serializers.SerializerMethodField()
    coversrc = serializers.SerializerMethodField()
    shopname = serializers.SerializerMethodField()
    price = serializers.SerializerMethodField()

    @staticmethod
    def get_name(obj):
        return obj.detail.cover.name

    @staticmethod
    def get_description(obj):
        return obj.detail.cover.description

    @staticmethod
    def get_coversrc(obj):
        return str(obj.detail.cover.src)

    @staticmethod
    def get_shopname(obj):
        return obj.detail.shop.name

    @staticmethod
    def get_price(obj):
        return obj.detail.cover.price

    class Meta:
        model = OrderGoods
        fields = "__all__"


class OrderDetailSerializer(serializers.ModelSerializer):
    created_time = serializers.CharField()
    pay_time = serializers.CharField()
    user = serializers.SerializerMethodField()
    phone = serializers.SerializerMethodField()
    nickname = serializers.SerializerMethodField()
    avatar = serializers.SerializerMethodField()
    status = serializers.CharField(source='get_status_display')
    ordergoods = OrderGoodsSerializer(many=True)

    @staticmethod
    def get_user(obj):
        return obj.user_id.studentid

    @staticmethod
    def get_phone(obj):
        return obj.user_id.phone

    @staticmethod
    def get_nickname(obj):
        return obj.user_id.nickname

    @staticmethod
    def get_avatar(obj):
        return str(obj.user_id.avatar)

    class Meta:
        model = OrderInfo
        fields = ['id', 'created_time', 'pay_time', 'order_sn', 'status', 'total_amount', 'total_count', 'user',
                  'nickname', 'avatar', 'phone', 'ordergoods']


class OrderPagnation(PageNumberPagination):
    page_query_param = 'page'
    page_size_query_param = 'size'
    max_page_size = 20


class OrderView(RetrieveModelMixin, ListModelMixin, GenericViewSet):
    queryset = OrderInfo.objects.all()
    serializer_class = OrderListSerializer
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filterset_class = OrderFilter
    pagination_class = OrderPagnation
    ordering = ['id', 'user_id__studentid', 'status', '-pay_time', '-created_time']

    def get_serializer_class(self):
        if self.action == "list":
            return OrderListSerializer
        else:
            return OrderDetailSerializer
